/*
 * Created on May 29, 2005
 */
package org.vectrics.recruiter.user.resume.email;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.vectrics.common.db.OwnedObject;
import org.vectrics.common.db.RecordObject;
import org.vectrics.domain.Domain;
import org.vectrics.recruiter.user.Resume;
import org.vectrics.user.Person;

/**
 * @author MMoore
 */
public class EmailedResumeInfo implements RecordObject, OwnedObject {
	private transient Logger log = Logger.getLogger(this.getClass());
	
	public static final String STATUS_NEW = "new";
	public static final String STATUS_PARTIAL_PROCESSED = "partial";
	public static final String STATUS_PROCESSED = "processed";
	public static final String STATUS_REJECTED_SPAM = "spam";
	public static final String STATUS_REJECTED_BAD_CANDIDATE = "bad-cand";
	public static final String STATUS_DISPOSED = "disposed";
	
	private Long id = null;
	private String fromEmailAddress = null;
	private String fromEmailName = null;
	private String emailSubject = null;
	private Date dateSent = null;
	private String firstName = null;
	private String middleName = null;
	private String lastName = null;
	private String phoneNumber = null;
	private Collection attachments = new HashSet();
	private String statusCode = STATUS_NEW; 
	private Domain domain = null;
	private Person person = null;

	public EmailedResumeInfo() {
		super();
	}
	
	/**
	 * @return Returns the id.
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id The id to set.
	 */
	public void setId(Long id) {
		this.id = id;
	}
	
	
	/**
	 * @return Returns the domain.
	 */
	public Domain getDomain() {
		return domain;
	}


	/**
	 * @param domain The domain to set.
	 */
	public void setDomain(Domain domain) {
		this.domain = domain;
	}


	/**
	 * @return Returns the dateSent.
	 */
	public Date getDateSent() {
		return dateSent;
	}

	/**
	 * @param dateSent The dateSent to set.
	 */
	public void setDateSent(Date dateSent) {
		this.dateSent = dateSent;
	}
	
	/**
	 * @return Returns the firstName.
	 */
	public String getFirstName() {
		return firstName;
	}
	
	/**
	 * @param firstName The firstName to set.
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	/**
	 * @return Returns the fromEmailAddress.
	 */
	public String getFromEmailAddress() {
		return fromEmailAddress;
	}
	
	/**
	 * @param fromEmailAddress The fromEmailAddress to set.
	 */
	public void setFromEmailAddress(String fromEmailAddress) {
		this.fromEmailAddress = fromEmailAddress;
	}
	
	/**
	 * @return Returns the fromEmailName.
	 */
	public String getFromEmailName() {
		return fromEmailName;
	}
	
	/**
	 * @param fromEmailName The fromEmailName to set.
	 */
	public void setFromEmailName(String fromEmailName) {
		this.fromEmailName = fromEmailName;
	}
	
	/**
	 * @return Returns the lastName.
	 */
	public String getLastName() {
		return lastName;
	}
	
	/**
	 * @param lastName The lastName to set.
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	/**
	 * @return Returns the middleName.
	 */
	public String getMiddleName() {
		return middleName;
	}
	/**
	 * @param middleName The middleName to set.
	 */
	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}
	/**
	 * @return Returns the phoneNumber.
	 */
	public String getPhoneNumber() {
		return phoneNumber;
	}
	/**
	 * @param phoneNumber The phoneNumber to set.
	 */
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	/**
	 * @return Returns the attachments.
	 */
	public Collection getAttachments() {
		return attachments;
	}
	
	/**
	 * @param attachments The attachments to set.
	 */
	protected void setAttachments(Collection attachments) {
		this.attachments = attachments;
	}
	
	public EmailedResumeAttachment addAttachment() {
		EmailedResumeAttachment attachment = new EmailedResumeAttachment();
		attachments.add(attachment);
		attachment.setEmailedResumeInfo(this);
		attachment.setDomain(this.getDomain());
		return(attachment);
	}

	/**
	 * @return Returns the emailSubject.
	 */
	public String getEmailSubject() {
		return emailSubject;
	}

	/**
	 * @param emailSubject The emailSubject to set.
	 */
	public void setEmailSubject(String emailSubject) {
		this.emailSubject = emailSubject;
	}

	
	

	/**
	 * @return Returns the person.
	 */
	public Person getPerson() {
		return person;
	}


	/**
	 * @param person The person to set.
	 */
	public void setPerson(Person person) {
		this.person = person;
	}


	/**
	 * @return Returns the statusCode.
	 */
	public String getStatusCode() {
		return statusCode;
	}


	/**
	 * @param statusCode The statusCode to set.
	 */
	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getStatusPhraseCode() {
		String result = null;
		if (this.statusCode.equals(EmailedResumeInfo.STATUS_DISPOSED)) 
			result = "phrase.email.resume.status.disposed";
		else if (this.statusCode.equals(EmailedResumeInfo.STATUS_NEW)) 
			result = "phrase.email.resume.status.new";
		else if (this.statusCode.equals(EmailedResumeInfo.STATUS_PARTIAL_PROCESSED)) 
			result = "phrase.email.resume.status.partial";
		else if (this.statusCode.equals(EmailedResumeInfo.STATUS_REJECTED_SPAM)) 
			result = "phrase.email.resume.status.spam";
		else if (this.statusCode.equals(EmailedResumeInfo.STATUS_REJECTED_BAD_CANDIDATE)) 
			result = "phrase.email.resume.status.bad.candidate";
		else 
			log.warn("Do not know what phrase code to use for status: " + this.getStatusCode());
		return result;
	}

	/**
	 * Method that finds the attachment that is categorized as being a resume. 
	 * @return The found resume, null if no attached resume found.
	 */
	public Resume findAttachedResume() {
		Resume foundResume = null;
		Iterator iter = this.getAttachments().iterator();
		while ((iter.hasNext()) && (foundResume == null)) {
			EmailedResumeAttachment attachment = (EmailedResumeAttachment)iter.next();
			if (attachment.getResume() != null) {
				foundResume = attachment.getResume();
			}
		}
		return(foundResume);
	}
	
}
